/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2022-12-16 10:52:10
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-09-25 15:54:58
 * @FilePath: \cxl-admin\src\pages\operation\product\list\index.ts
 */

import { productPriceDelete } from '@/apis/operation/price'
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { ElMessage, ElMessageBox } from 'element-plus'
import { reactive } from 'vue'
import * as tableConfig from './config.json'
import { enableProduct, listProduct } from '@/apis/operation/product'

export class PriceData extends BaseData implements BaseMethod {
    //单笔弹窗数据
    singleDialog: any = reactive({
        visible: false,
        type: 'create',
        priceType: 3, //3 单笔 | 套餐 2次数包 1充值赠送
        data: null
    })
    //套餐弹窗数据
    setMealDialog: any = reactive({
        visible: false,
        type: 'create',
        priceType: 3, //3 单笔 | 套餐 2次数包 1充值赠送
        data: null
    })

    //搜索值 2 =默认全部 1 =启用 0= 禁用
    searchValue = '2'

    //存放每个表格的列字段
    propName: any[][] = [[], []]

    constructor() {
        super(tableConfig)

        this.getData()
    }

    onOperation = (arg?: any) => {
        const { type, data } = arg
        if (type === 'edit') {
            this.singleDialog.visible = true
            this.singleDialog.type = type
            this.singleDialog.data = data
        } else if (type === 'create') {
            this.singleDialog.visible = true
            this.singleDialog.type = type
        } else if (type === 'delete') {
            ElMessageBox.confirm('确定要删除该价格吗？', '删除', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            })
                .then(() => {
                    productPriceDelete(data.id).then(res => {
                        if (res.data.value === 200) {
                            ElMessage.success('操作成功！')
                            this.getData()
                        } else {
                            ElMessage.error(res.data.desc)
                        }
                    })
                })
                .catch(() => {
                    //
                })

            console.log(data)
        } else if (type === 'switch') {
            enableProduct(data.id, Number(data.productType), Number(data.status)).then(res => {
                console.log(res)
                if (res.data.value === 200 || res.data.value === 31001 || res.data.value === 31002) {
                    this.getData()
                }
            })
        }
    }
    /**
     * 监听点击搜索按钮
     * @param res
     */
    onSearch = (res: any) => {
        this.pageData.pagination.pageNo = 1
        this.searchValue = res.status
        this.getData()
    }

    /**
     * 监听点击重置按钮
     */
    onReset = () => {
        this.pageData.pagination.pageNo = 1
        this.searchValue = '2'
        this.getData()
    }

    /**
     * 监听分页发生改变
     * @param index
     */
    onPaginationChange = (index: number) => {
        this.pageData.pagination.pageNo = index
        this.getData()
    }

    /**
     * 监听选择每页显示个数
     * @param size
     */
    onPaginationSize = (size: number) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = size
        this.getData()
    }

    /**
     * 点击新增价格按钮菜单 根据传入的参数判断是新增，还是编辑
     * @param type
     * @param index
     */
    onPriceConfig = (type: string, index?: number) => {}

    /**
     * 获取表格数据方法
     * @param status 状态（0.禁用；1.启用）
     */
    getData = () => {
        const obj: any = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        //status 状态（0.禁用；1.启用）
        if (Number(this.searchValue) !== 2 && this.searchValue !== '') {
            obj.status = Number(this.searchValue)
        }

        listProduct(obj).then(res => {
            if (res.data.value === 200) {
                this.pageData.table.data.length = 0
                const arr: any[] = JSON.parse(JSON.stringify(res.data.data.records))
                arr.forEach(item => {
                    item.status = item.status == 1 ? true : false
                    item.priceType = item.type
                    item.validity = -1
                    item.price = item.singlePrice
                    item.salesPrice = (Number(item.basicPrice) - Number(item.singlePrice)).toFixed(2)
                })
                this.pageData.table.data.push(...arr)
                this.pageData.pagination.total = res.data.data.total
            }
        })
    }

    /**
     * 监听编辑弹窗隐藏
     */
    onDialogClose = () => {
        //单笔
        this.singleDialog.visible = false
        this.getData()
    }
}
